<template>
  <div>
        <header-cmp><span v-if="status">添加地址</span><span v-else>编辑地址</span></header-cmp>
        <div class="xq">
            <van-address-edit
            v-if="status"
            :area-list="areaList"
            show-set-default
            :search-result="searchResult"
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            @save="onSave"
            />
            <van-address-edit
            v-else
            :area-list="areaList"
            :address-info="AddressInfo "
            show-delete
            show-set-default
            :area-columns-placeholder="['请选择', '请选择', '请选择']"
            @save="onUpdate"
            @delete="onDelete"
            />
        </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
import HeaderCmp from '../components/HeaderCmp.vue'
import axios from  'axios';
import {mapState} from 'vuex';
export default {
    data() {
        return {
            status:false,
            areaList:{
                province_list: {
                    110000: '北京市',
                    120000: '天津市',
                    130000: '湖南省'
                },
                city_list: {
                    110100: '北京市',
                    120100: '天津市',
                    130100: '邵阳市',
                    130200: '长沙市',
                    130300: '株洲市',
                    130400: '湘潭市',
                },
                county_list: {
                    110101: '东城区',
                    110102: '西城区',
                    130101: '大祥区',
                    130201: '岳麓区',
                    130301: '大丰',
                    130401: '湘潭市',
                },
                
            },
            searchResult: [],
        };
    },
    created(){
        let params=this.$route.params
        if(params.key==='add'){
            this.status=true
        }else{
            params.obj.isDefault=params.obj.isDefault === 1 ? true : false
            this.AddressInfo=params.obj
        }
    },
    computed:{
        ...mapState({
            token:state=>state.user.token
        })
    },
    methods: {
        onUpdate(content){
            content.isDefault = content.isDefault===true ? 1 : 0
            axios({
                url:'/ajax/updateAddress',
                method:'post',
                data:{
                    token:this.token,
                    ...content
                }
            }).then(({data})=>{
                if(data.success){
                    Toast(data.msg);
                    this.$router.push({name:'ain'})
                } 
            })
        },
        onSave(content) {
            content.isDefault = content.isDefault===true ? 1 : 0
            axios({
                url:'/ajax/addAddress',
                method:'post',
                data:{
                    token:this.token,
                    ...content
                }
            }).then(({data})=>{
                if(data.success){
                    Toast(data.msg);
                    this.$router.push({name:'ain'})
                } 
            })
            
        },
        onDelete(content){
            content.isDefault = content.isDefault===true ? 1 : 0
            let id = content.id
            axios({
                url:'/ajax/deleteAddress',
                method:'post',
                data:{
                    token:this.token,
                    id
                }
            }).then(({data})=>{
                if(data.success){
                    Toast(data.msg);
                    this.$router.push({name:'ain'})
                } 
            })
        }
    },
    components:{
        HeaderCmp
        
    }
}
</script>

<style lang="scss" scoped>
.xq{
    .van-address-edit{
        padding: 0;
    }
}
</style>